package com.ubao.activity.main;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.view.GestureDetector.OnGestureListener;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.ListView;

import com.ubao.R;
import com.ubao.ui.slidingmenu.MyHorizontalScrollView;
import com.ubao.ui.slidingmenu.MyHorizontalScrollView.SizeCallback;
import com.ubao.ui.slidingmenu.ViewUtils;
import com.ubao.ui.titlebar.GTitleBar;

/**
 * This demo uses a custom HorizontalScrollView that ignores touch events, and therefore does NOT allow manual scrolling.
 * 
 * The only scrolling allowed is scrolling in code triggered by the menu button.
 * 
 * When the button is pressed, both the menu and the app will scroll. So the menu isn't revealed from beneath the app, it
 * adjoins the app and moves with the app.
 */
public class HorzScrollWithListMenu    {
//    MyHorizontalScrollView scrollView;
//    View menu;
//    View app;
//    ImageView btnSlide;
//    boolean menuOut = false;
//    Handler handler = new Handler();
//    int btnWidth;
//    GTitleBar mTitleBar;
//    @Override
//    public void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);

//        LayoutInflater inflater = LayoutInflater.from(this);
//        scrollView = (MyHorizontalScrollView) inflater.inflate(R.layout.horz_scroll_with_list_menu, null);
//        setContentView(scrollView);
//
//        menu = inflater.inflate(R.layout.horz_scroll_menu, null);
//        app = inflater.inflate(R.layout.horz_scroll_app, null);
//        mTitleBar = (GTitleBar) app.findViewById(R.id.mTitleBar);
//
//        ListView listView = (ListView) app.findViewById(R.id.list);
//        ViewUtils.initListView(this, listView, "Item ", 30, android.R.layout.simple_list_item_1);
//
//        listView = (ListView) menu.findViewById(R.id.list);
//        ViewUtils.initListView(this, listView, "Menu ", 30, android.R.layout.simple_list_item_1);
//
////        btnSlide = (ImageView) tabBar.findViewById(R.id.BtnSlide);
//        mTitleBar.initRightButton("菜单", R.drawable.titlebar_menu, new ClickListenerForScrolling(scrollView, menu));
////        btnSlide.setOnClickListener(new ClickListenerForScrolling(scrollView, menu));
//        System.out.println("b----------->");
//        final View[] children = new View[] { menu, app };
//
//        // Scroll to app (view[1]) when layout finished.
//        int scrollToViewIdx = 1;
//        scrollView.initViews(children, scrollToViewIdx, new SizeCallbackForMenu(mTitleBar.getLeftButton()));
//    }

    /**
     * Helper for examples with a HSV that should be scrolled by a menu View's width.
     */
    static class ClickListenerForScrolling implements OnClickListener {
        HorizontalScrollView scrollView;
        View menu;
        /**
         * Menu must NOT be out/shown to start with.
         */
        boolean menuOut = false;

        public ClickListenerForScrolling(HorizontalScrollView scrollView, View menu) {
            super();
            this.scrollView = scrollView;
            this.menu = menu;
        }

        @Override
        public void onClick(View v) {
            Context context = menu.getContext();

            int menuWidth = menu.getMeasuredWidth();

            // Ensure menu is visible
            menu.setVisibility(View.VISIBLE);

            if (!menuOut) {
                // Scroll to 0 to reveal menu
                int left = 0;
                scrollView.smoothScrollTo(left, 0);
            } else {
                // Scroll to menuWidth so menu isn't on screen.
                int left = menuWidth;
                scrollView.smoothScrollTo(left, 0);
            }
            menuOut = !menuOut;
        }
    }

    /**
     * Helper that remembers the width of the 'slide' button, so that the 'slide' button remains in view, even when the menu is
     * showing.
     */
    static class SizeCallbackForMenu implements SizeCallback {
        int btnWidth;
        View btnSlide;

        public SizeCallbackForMenu(View btnSlide) {
            super();
            this.btnSlide = btnSlide;
        }

        @Override
        public void onGlobalLayout() {
            btnWidth = btnSlide.getMeasuredWidth();
            System.out.println("btnWidth=" + btnWidth);
        }

        @Override
        public void getViewSize(int idx, int w, int h, int[] dims) {
            dims[0] = w;
            dims[1] = h;
            final int menuIdx = 0;
            if (idx == menuIdx) {
                dims[0] = w - btnWidth;
            }
        }
    }
//
//	@Override
//	public boolean onDown(MotionEvent e) {
//		// TODO Auto-generated method stub
//		System.out.println("down:" + e.getX() + " " + e.getY());
//		return false;
//	}
//
//	@Override
//	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
//			float velocityY) {
//		// TODO Auto-generated method stub
//		System.out.println("down:" + e1.getX() + " " + e2.getX());
//		return false;
//	}
//
//	@Override
//	public void onLongPress(MotionEvent e) {
//		// TODO Auto-generated method stub
//		
//	}
//
//	@Override
//	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
//			float distanceY) {
//		// TODO Auto-generated method stub
//		System.out.println("down:" + e1.getY() + " " + e2.getY());
//		return false;
//	}
//
//	@Override
//	public void onShowPress(MotionEvent e) {
//		// TODO Auto-generated method stub
//		
//	}
//
//	@Override
//	public boolean onSingleTapUp(MotionEvent e) {
//		// TODO Auto-generated method stub
//		return false;
//	}
}
